

; Part of Schiero development utils
; Takes an animation (gif for example), applies color->alpha and outputs each frame to seperate file with naming to ease importing into the game



; Layers To Transparent PNGs
;   applies color->alpha for each layer and then saves to it to a seperate file
(define (script-fu-layers-to-transparent-pngs image transparency-color start end reverse-order pad)
  (let* ((basename   (car (gimp-image-get-filename image)))
         (query      (gimp-image-get-layers image))
         (layers     (car (cdr query)))
         (num-layers (car query)))
    ; set start and end
    (if (string=? start "start")
        (set! start 0)
        (set! start (string->number start)))
    (if (string=? end "end")
        (set! end (- num-layers 1))
        (set! end (string->number end)))
    (when reverse-order
      (let ((temp start))
        (set! start end)
        (set! end temp)))
    (if (or (not (number? start)) (not (number? end)))
        (gimp-message "Invalid start or end!")
        (if (or (< start 0) (< end 0) (>= start num-layers) (>= end num-layers))
            (gimp-message "Start or end is out of range!")
            (begin
              ; clean up name
              (if (string=? "" basename)
                  ; if no name, generate
                  (set! filename (string-append
                                  (car (gimp-temp-name ""))
                                  (car (gimp-image-get-name image))))
                  ; else, remove extension (assume 3 characters)
                  (set! basename (substring basename 0 (- (string-length basename) (string-length ".xxx")))))
              ; loop through layers
              (let loop ((layer start)
                         (out 0))
                ; copy layer into buffer
                (gimp-edit-copy (vector-ref layers layer))
                ; create new image from buffer
                (let* ((img      (car (gimp-edit-paste-as-new)))
                       (img-name (string-append basename
                                                (if pad
                                                    (let pad-loop ((pads (- (string-length (number->string num-layers)) (string-length (number->string out))))
                                                                   (res ""))
                                                      (if (= pads 0) res (pad-loop (- pads 1)(string-append res "0"))))
                                                    "")
                                                (number->string out)
                                                ".png")))
                  ; convert to rgb
                  (when (eq? (gimp-drawable-is-rgb (vector-ref (cadr (gimp-image-get-layers img)) 0)) FALSE)
                    (gimp-image-convert-rgb img))
                  ; make transparent
                  (plug-in-colortoalpha TRUE
                                        img
                                        (vector-ref (cadr (gimp-image-get-layers img)) 0)
                                        transparency-color)
                  ; save
                  (file-png-save TRUE
                                 img
                                 (vector-ref (cadr (gimp-image-get-layers img)) 0)
                                 img-name
                                 img-name
                                 TRUE 9 FALSE TRUE FALSE FALSE TRUE)
                  ; clean up
                  (gimp-image-delete img))
                ; loop
                (unless (= layer end)
                  (loop (rotate-in-range num-layers ((if reverse-order - +) layer 1)) (+ out 1)))))))))

  
; Rotate In Range
(define (rotate-in-range size value)
  (let rotate-in-range-loop ((res value))
    (if (< res 0)
        (rotate-in-range-loop (+ res size))
        (if (>= res size)
            (rotate-in-range-loop (- res size))
            res))))


; register script
(script-fu-register "script-fu-layers-to-transparent-pngs"
                    "Layers to transparent PNGs"
                    "Applies color->alpha on layers and saves them to separate .png files"
                    "Robert Cranston"
                    "Robert Cranston"
                    "2010-05-18"
                    ""
                    SF-IMAGE  "Image"                1
                    SF-COLOR  "Transparency color"   (list 0 0 0)
                    SF-STRING "Start"                "start"
                    SF-STRING "End"                  "end"
                    SF-TOGGLE "Reverse order"        FALSE
                    SF-TOGGLE "Pad name with zeroes" TRUE)


; add to menu
(script-fu-menu-register "script-fu-layers-to-transparent-pngs"
                         "<Image>/Layer")



